<template>
	<view class="Global">
		<bgImage></bgImage>
		<view class="title">{{title}}</view>
		<view class="content">{{content}}</view>
		<view class="loginTips">{{loginTips}}</view>
		<view class="content">
			<input class="inputAccout" type="text" value="" @input="onInputAccount" :placeholder="inputAccount" />
		</view>
		<view class="content">
			<input class="inputPass" type="text" value="" @input="onInputPass" :placeholder="inputPass" />
		</view>

		<view class="contentBottom">

			<button @click="loginClick()" style="color:#ffffff" class="login" type="default">{{login}}</button>
		</view>


		<view class="contentBottom">

			<!-- <button @click="loginClick()" style="color:#ffffff" class="login" type="default">{{login}}</button> -->

			<!-- #ifdef MP-WEIXIN -->
			<button class="login" type="primary"  open-type="getPhoneNumber"
				@getphonenumber="getPhoneNumber">{{quicklyLogin}}</button>
			<!-- #endif -->
		</view>

	</view>
</template>

<script>
	import bgImage from '@/components/backgroundImage.vue'

	import {

		login
	} from '@/pages/api/api.js'
  import {getInfo, timeDeviceList,loginWechat} from "../api/api";


	export default {
		components: {

			bgImage
		},
		data() {
			return {
				title: '欢迎来到',
				content: '好好用(制衣版)',
				loginTips: '登录您的账号',
				inputAccount: '请输入账号',
				inputPass: '请输入密码',
				login: '登录',
				quicklyLogin:'手机号一键登录',


				user: {
					username: '',
					password: '',

				},

				// 				一键登录信息
				user: {
					code: '',
					p_code: '',
					storeId: '',
					tenantId: '',
					encryptedData:''

				}


			}
		},

		onLoad(e) {

				const app = getApp();
				this.user.storeId = app.globalData.storeId
				this.user.tenantId = app.globalData.tenantId

			console.log('获取登录信息',e);
			/* uni.navigateTo({
				url: '/subPackA/quickLogin/quickLogin',
				success: res => {},
				fail: () => {},
				complete: () => {}
			}); */

		},
		methods: {

			// 一键登录
			quickLogin() {

				// console.log('打印登录的账密',this.user);


				loginWechat(this.user).then((res) => {
					console.log('11获取数据成功', res);

					if (res.code == 200) {
						// 保存token
						uni.setStorageSync("token", res.token)
						// getInfo().then((result) => {
						//   if (result.code == 200) {
						//     uni.setStorageSync("user", result.user)
						//     uni.setStorageSync("store", result.store)
						//     uni.setStorageSync("roles", result.roles)
						//     uni.setStorageSync("permissions", result.permissions)
						//   }
						// })
						// console.log('获取token',uni.getStorageSync('token'));
						// 返回成功,则跳转首页
						uni.switchTab({
							url: '/pages/index/index',
							success: res => {},
							fail: () => {},
							complete: () => {}
						});
					} else {
						// 提示错误信息
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}


				}).catch((err) => {
					// console.error('11获取数据失败', err);
					uni.showToast({
						title: err.msg
					});
				});


			},

			getPhoneNumber(e) {
				uni.login({
					provider: "weixin",
					success: res => {
						console.log('获取登录信息', res, e);
						this.user.code = res.code
						this.user.encryptedData = e.detail.encryptedData
						this.user.p_code = e.detail.iv
						this.quickLogin()
					}
				})

			},
			// 获取账号输入框的值
			onInputAccount(e){
				this.user.username=e.detail.value
			},
			// 获取密码框输入的值
			onInputPass(e){
				this.user.password=e.detail.value
			},

			// 登录
			loginClick() {

				// console.log('打印登录的账密',this.user);


				login(this.user).then((res) => {
					// console.log('11获取数据成功', res);

					if (res.code == 200) {
						// 保存token
						uni.setStorageSync("token", res.token)
            // getInfo().then((result) => {
            //   if (result.code == 200) {
            //     uni.setStorageSync("user", result.user)
            //     uni.setStorageSync("store", result.store)
            //     uni.setStorageSync("roles", result.roles)
            //     uni.setStorageSync("permissions", result.permissions)
            //   }
            // })
						// console.log('获取token',uni.getStorageSync('token'));
						// 返回成功,则跳转首页
						uni.switchTab({
							url: '/pages/index/index',
							success: res => {},
							fail: () => {},
							complete: () => {}
						});
					}else{
						// 提示错误信息
						uni.showToast({
							title: res.msg,
							icon:'none'
						});
					}


				}).catch((err) => {
					// console.error('11获取数据失败', err);
					uni.showToast({
						title: err.msg
					});
				});


			}

		}
	}
</script>

<style>
	.contentBottom {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100%;
		width: 100%;

	}

	.bg {
		background-color: aqua;
		height: 100%;
		width: 100%;
	}

	.title {
		margin-top: 179rpx;
		text-align: center;
		font-size: 40rpx;
	}

	.content {
		margin-top: 18rpx;
		text-align: center;
		font-size: 60rpx;
		font-weight: bold;
	}

	.loginTips {
		margin-top: 137rpx;
		text-align: center;
		font-size: 40rpx;
		font-weight: bold;
	}

	.inputAccout {
		margin-top: 50rpx;
		width: 550rpx;
		height: 110rpx;
		background: #FFFFFF;
		border-radius: 30rpx;


		font-size: 28rpx;
		padding-left: 30rpx;
		text-align: left;
	}

	.inputPass {
		margin-top: 20rpx;


		width: 550rpx;
		height: 110rpx;
		background: #FFFFFF;
		border-radius: 30rpx;

		border-radius: 30rpx;
		font-size: 28rpx;
		padding-left: 30rpx;
		text-align: left;
	}

	.login {
		margin-top: 30rpx;
		width: 580rpx;
		height: 110rpx;

		border-radius: 30rpx;
		font-weight: 400;
		font-size: 36rpx;
		color: #FFFFFF;
		text-align: center;
		font-weight: bold;

		display: flex;
		justify-content: center;
		align-items: center;

		background: linear-gradient(90deg, #45A8FF, #0AE1C6);

	}
</style>
